<?php

namespace app\admin\controller;

use think\Db;
use think\Validate;

class ActivityCenter extends Common {
	public function chatdiscountconfig()
	{
        if (request()->isAjax()) {
        	$post = input('post.');
        	if (!$post['starttime']) return ['err'=>'时间错误'];
        	if (!$post['endtime']) return ['err'=>'时间错误'];
        	if (strtotime($post['endtime']) <= strtotime($post['starttime'])) return ['err'=>'时间错误'];
        	if ($post['discount'] < 1 || $post['discount'] > 99) return ['err'=>'折扣错误'];
        	if (!$post['description']) return ['err'=>'活动描述错误'];
        	$res = Db::name('act_chat_discount_config')->where(['id'=>1])->update($post);
        	if ($res) return ['ret'=>'修改成功'];
        	return ['err'=>'修改失败'];
		} else {
			$data = Db::name('act_chat_discount_config')->find();
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

	public function chatdiscountuser()
	{

		Db::name('act_chat_discount_user')->where(['endtime'=>['<=', date('Y-m-d H:i:s')], 'state'=>0])->update(['state'=>1]);


		$where = [];


		$state = input('state/d');

		if ($state !== null) {
			$this->assign('state', $state);
			$where['state'] = $state;
		}


		$search = input('search');
		$name = input('name');
		if ($search && $name) {
			$where[$search] = $name;
		}
		$data = Db::name('act_chat_discount_user')->master()->where($where)->order('id desc')->paginate(_getCachePages(),false,['query'=>request()->param()]);
		$page = $data->render();

		$this->assign('page',$page);
		$this->assign('data',$data);
		return $this->fetch();
	}

	public function chatdiscountuserdel($id)
	{
		$discount = Db::name('act_chat_discount_user')->where(['user_id'=>$id, 'state'=>0])->find();
		if (!$discount) return ['state'=>0,'msg'=>'错误操作'];

    	$res = Db::name('act_chat_discount_user')->where(['id'=>$discount['id']])->update(['state'=>-1, 'expire_time'=>time()]);
    	if ($res) return ['state'=>1,'msg'=>'操作成功'];
    	return ['state'=>0,'msg'=>'操作失败'];
	}
}